package com.cyzy.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 摄像头集成服务
 * 与camera-control服务交互
 */
@Service
public class CameraIntegrationService {
    
    @Value("${camera-service.url:http://localhost:8081}")
    private String cameraServiceUrl;
    
    private final RestTemplate restTemplate = new RestTemplate();
    
    /**
     * 启动摄像头进行人脸识别
     */
    public Map<String, Object> startFaceRecognition(String userId) {
        try {
            String url = cameraServiceUrl + "/api/camera/start-face-recognition";
            Map<String, Object> request = new HashMap<>();
            request.put("userId", userId);
            
            // 调用摄像头服务
            Map<String, Object> response = restTemplate.postForObject(url, request, Map.class);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "摄像头服务连接失败: " + e.getMessage());
            return errorResponse;
        }
    }
    
    /**
     * 停止摄像头
     */
    public Map<String, Object> stopCamera(String userId) {
        try {
            String url = cameraServiceUrl + "/api/camera/stop";
            Map<String, Object> request = new HashMap<>();
            request.put("userId", userId);
            
            Map<String, Object> response = restTemplate.postForObject(url, request, Map.class);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "停止摄像头失败: " + e.getMessage());
            return errorResponse;
        }
    }
    
    /**
     * 获取摄像头状态
     */
    public Map<String, Object> getCameraStatus(String userId) {
        try {
            String url = cameraServiceUrl + "/api/camera/status?userId=" + userId;
            
            Map<String, Object> response = restTemplate.getForObject(url, Map.class);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("status", "disconnected");
            errorResponse.put("message", "获取摄像头状态失败: " + e.getMessage());
            return errorResponse;
        }
    }
    
    /**
     * 进行人脸验证（用于实名认证）
     */
    public Map<String, Object> verifyFace(String userId, String faceImageBase64) {
        try {
            String url = cameraServiceUrl + "/api/camera/verify-face";
            Map<String, Object> request = new HashMap<>();
            request.put("userId", userId);
            request.put("faceImage", faceImageBase64);
            
            Map<String, Object> response = restTemplate.postForObject(url, request, Map.class);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "人脸验证失败: " + e.getMessage());
            return errorResponse;
        }
    }
}
